package com.bosssoft.ct.springcloud.controller;

import com.bosssoft.ct.springcloud.pojo.dto.UserDTO;
import com.bosssoft.ct.springcloud.pojo.vo.UserVO;
import com.bosssoft.ct.springcloud.service.UserService;
import com.bosssoft.ct.springcloud.utils.http.HttpResult;
import com.bosssoft.ct.springcloud.utils.token.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录逻辑控制器
 * @version 1.0
 * @author ChenTong
 * @date 2020/6/23 21:47
 * @copyright
 * @modified
 * @see
 * @since
 **/
@RestController
@Slf4j
public class Login {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public HttpResult login(@RequestBody UserDTO user, HttpServletResponse response){

        UserVO realUser = userService.login(user);
        //新建token
        String token = TokenUtil.createJwtToken(realUser.getUsername(),5000000);
        //将token信息添加到响应头中方便前端存放以及获取
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        response.setHeader("Authorization",token);

        return HttpResult.ok(realUser);
    }
}
